package com.psy.counseling.controller;

import cn.hutool.core.util.StrUtil;
import com.auth0.jwt.JWT;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.psy.counseling.utils.Result;
import com.psy.counseling.entity.LoginLog;
import com.psy.counseling.entity.User;
import com.psy.counseling.service.LoginLogService;
import com.psy.counseling.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
/**
 * <p>
 *  登录日志管理
 * </p>
 *
 * @author 陈灿
 * @since 2022-05-31
 */
@RestController
@RequestMapping("/api/log")
public class LogController {
    @Resource
    private LoginLogService loginLogService;
    @Resource
    private HttpServletRequest request;
    @Resource
    private UserService userService;

    public User getUser() {
        String token = request.getHeader("token");
        String username = JWT.decode(token).getAudience().get(0);
        return userService.getOne(Wrappers.<User>lambdaQuery().eq(User::getUsername, username));
    }

    @PostMapping
    public Result<?> save(@RequestBody LoginLog log) {
        return Result.success(loginLogService.save(log));
    }

    @PutMapping
    public Result<?> update(@RequestBody LoginLog log) {
        return Result.success(loginLogService.updateById(log));
    }

    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Long id) {
        loginLogService.removeById(id);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result<?> findById(@PathVariable Long id) {
        return Result.success(loginLogService.getById(id));
    }

    @GetMapping
    public Result<?> findAll() {
        return Result.success(loginLogService.list());
    }

    @GetMapping("/page")
    public Result<?> findPage(@RequestParam(required = false, defaultValue = "") String name,
                              @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                              @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        LambdaQueryWrapper<LoginLog> query = Wrappers.<LoginLog>lambdaQuery().orderByDesc(LoginLog::getId);
        if (StrUtil.isNotBlank(name)) {
            query.like(LoginLog::getContent, name);
        }
        return Result.success(loginLogService.page(new Page<>(pageNum, pageSize), query));
    }
    @GetMapping("/newpage")
    public Result<?> findNewPage(@RequestParam(required = false,defaultValue = "1") Integer pageNum,
                                 @RequestParam(required = false,defaultValue = "20") Integer pageSize){
        LambdaQueryWrapper<LoginLog> logLambdaQueryWrapper = new LambdaQueryWrapper<LoginLog>().orderByDesc(LoginLog::getId);
            return Result.success(loginLogService.page(new Page<>(pageNum,pageSize),logLambdaQueryWrapper));
    }

}
